# Copyright 2025 Enactic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.22)
project(openarm_can VERSION 1.0.0)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

include(GNUInstallDirs)

# Create the main library
add_library(
  openarm_can
  src/openarm/can/socket/arm_component.cpp
  src/openarm/can/socket/gripper_component.cpp
  src/openarm/can/socket/openarm.cpp
  src/openarm/canbus/can_device_collection.cpp
  src/openarm/canbus/can_socket.cpp
  src/openarm/eyou_motor/eyou_motor.cpp
  src/openarm/eyou_motor/eyou_motor_control.cpp
  src/openarm/eyou_motor/eyou_motor_device.cpp
  src/openarm/eyou_motor/eyou_motor_device_collection.cpp)
set_target_properties(openarm_can PROPERTIES POSITION_INDEPENDENT_CODE ON)
set(USE_FILE_SET_HEADERS ON)
# Meson doesn't support FILE_SET TYPE HEADERS...
# if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.23)
#   set(USE_FILE_SET_HEADERS TRUE)
# endif()
if(USE_FILE_SET_HEADERS)
  target_sources(
    openarm_can
    PUBLIC FILE_SET
           HEADERS
           TYPE
           HEADERS
           BASE_DIRS
           include
           FILES
           include/openarm/can/socket/arm_component.hpp
           include/openarm/can/socket/gripper_component.hpp
           include/openarm/can/socket/openarm.hpp
           include/openarm/canbus/can_device.hpp
           include/openarm/canbus/can_device_collection.hpp
           include/openarm/canbus/can_socket.hpp
           include/openarm/eyou_motor/eyou_motor.hpp
           include/openarm/eyou_motor/eyou_motor_constants.hpp
           include/openarm/eyou_motor/eyou_motor_control.hpp
           include/openarm/eyou_motor/eyou_motor_device.hpp
           include/openarm/eyou_motor/eyou_motor_device_collection.hpp)
  install(
    TARGETS openarm_can
    EXPORT openarm_can_export
    FILE_SET HEADERS)
else()
  target_include_directories(
    openarm_can PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
                       $<INSTALL_INTERFACE:include>)
  install(TARGETS openarm_can EXPORT openarm_can_export)
  install(DIRECTORY include/openarm TYPE INCLUDE)
endif()

# CMake package
add_executable(motor-check setup/motor_check.cpp)
target_link_libraries(motor-check openarm_can)

# Add motor control example executable
add_executable(openarm-demo examples/demo.cpp)
target_link_libraries(openarm-demo openarm_can)

add_executable(openarm-demo_r examples/demo_r.cpp)
target_link_libraries(openarm-demo_r openarm_can)

add_executable(openarm-demo_w examples/demo_w.cpp)
target_link_libraries(openarm-demo_w openarm_can)

